<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>css3 开关</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      background-color: #181818;
    }

    .switch_contanier {
      /* 盒子居中 */
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    .hidden {
      display: none;
    }

    .switch_label {
      position: relative;
      display: block;
      height: 22px;
      width: 40px;
      background-color: #000;
      border-radius: 13px;
      cursor: pointer;
      border: 2px solid #404040;
      transition: all .2s ease;
    }

    .switch_label:after {
      content: "";
      display: block;
      position: absolute;
      left: -2px;
      top: -2px;
      width: 22px;
      height: 22px;
      border-radius: 100px;
      background-color: #a0a0a0;
      box-shadow: 0 1px 2px rgba(0, 0, 0, .4);
      transition: inherit;
    }

    .switch_label:active:after {
      width: 26px;
      background-color: #fff;
    }

    .switch_input:checked~label {
      background-color: #1db955;
    }

    .switch_input:checked~label:after {
      left: 16px;
      background-color: #fff;
    }

    .switch_input:checked~label:active:after {
      left: 12px;
    }
  </style>
</head>

<body>
  <div class="switch_contanier">
    <input type="checkbox" id="switch_for" class="switch_input hidden">
    <label for="switch_for" class="switch_label"></label>
  </div>
</body>

</html>